﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Media.Effects;
using System.Windows.Browser;
using Utility;
using Utility.Service;

namespace Site
{
    public partial class ProductList : UserControl
    {
        private string uploadpath;

        private int _sort;
        private int _classid;

        public int Sort
        {
            set { this._sort = value; }
        }
        public int ClassID
        {
            set { this._classid = value; }
        }

        /// <summary>
        /// 外发光效果
        /// </summary>
        private DropShadowEffect EffectShadow()
        {
            System.Windows.Media.Effects.DropShadowEffect ds = new System.Windows.Media.Effects.DropShadowEffect();
            ds.Color = Colors.White;
            ds.BlurRadius = 100;
            ds.ShadowDepth = 0;
            return ds;
        }

        public ProductList()
        {
            InitializeComponent();
            this.myMenu.Logo = "1";
            uploadpath = App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin"));
            this.tbkTitle.Effect = EffectShadow();
            this.Loaded += new RoutedEventHandler(ProductList_Loaded);
        }

        void ProductList_Loaded(object sender, RoutedEventArgs e)
        {
            //菜单下滑动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = -83;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, myMenu);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin();

            InitTitle(this._sort);
            InitCategory(this._sort);
        }

        #region InitPageTitle
        private void InitTitle(int id)
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadProductCompleted += new EventHandler<ReadProductCompletedEventArgs>(client_ReadProductCompleted);
            client.ReadProductAsync(id);
        }

        void client_ReadProductCompleted(object sender, ReadProductCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    this.tbkTitle.Text = e.Result.Title;
                    //生成面包屑导航(220V/350V)
                    this.lnkSort.Content = e.Result.Title;
                    this.lnkSort.Tag = e.Result.ID;
                }
            }
            else
            {
                throw e.Error;
            }
        }
        #endregion

        #region InitCategory
        private void InitCategory(int parent)
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadProductCategoriesCompleted += new EventHandler<ReadProductCategoriesCompletedEventArgs>(client_ReadProductCategoriesCompleted);
            client.ReadProductCategoriesAsync(parent);
        }

        /// <summary>
        /// 生成左侧导航
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_ReadProductCategoriesCompleted(object sender, ReadProductCategoriesCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    if (this._classid == 0)
                    {
                        this._classid = e.Result[0].ID;
                    }
                    #region 生成目录
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        Button btn = new Button();
                        btn.Name = e.Result[i].ID.ToString();
                        btn.Click += new RoutedEventHandler(btn_Click);
                        btn.Style = Application.Current.Resources["styleLeftNavButton"] as Style;
                        btn.Content = e.Result[i].Title;
                        this.spCategory.Children.Add(btn);

                        if (null != e.Result[i].Children)
                        {
                            #region 生成二级目录
                            StackPanel sp = new StackPanel();
                            Brush bk = new SolidColorBrush(Colors.LightGray);
                            sp.Background = bk;
                            sp.Margin = new Thickness(25, 0, 25, 0);
                            for (int j = 0; j < e.Result[i].Children.Count; j++)
                            {
                                HyperlinkButton lnk = new HyperlinkButton();
                                lnk.Name = e.Result[i].Children[j].ID.ToString();
                                lnk.Content = e.Result[i].Children[j].Title;
                                lnk.FontSize = 14;
                                Brush b = new SolidColorBrush(Colors.Black);
                                lnk.Foreground = b;
                                lnk.Margin = new Thickness(10, 5, 10, 0);
                                lnk.Click += new RoutedEventHandler(lnk_Click);
                                sp.Children.Add(lnk);
                            }
                            this.spCategory.Children.Add(sp);
                            #endregion
                        }
                    }
                    this.LayoutRoot.UpdateLayout();
                    #endregion

                    //产品列表
                    InitContent(this._classid);
                    //面包屑
                    InitTopNav(this._classid);
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void lnk_Click(object sender, RoutedEventArgs e)
        {
            InitContent(Convert.ToInt32((sender as HyperlinkButton).Name));
            InitTopNav(Convert.ToInt32((sender as HyperlinkButton).Name));
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            InitContent(Convert.ToInt32((sender as Button).Name));
            InitTopNav(Convert.ToInt32((sender as Button).Name));
        }
        #endregion

        #region InitContent
        private void InitContent(int classid)
        {
            spList.Children.Clear();
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadProductsCompleted+=new EventHandler<ReadProductsCompletedEventArgs>(client_ReadProductsCompleted);
            client.ReadProductsAsync(classid);
        }

        void client_ReadProductsCompleted(object sender, ReadProductsCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                StackPanel sp = null;
                if (null != e.Result)
                {
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        if (i % 4 == 0)
                        {
                            sp = new StackPanel();
                            sp.Orientation = Orientation.Horizontal;
                        }
                        BitmapImage img = new BitmapImage(new Uri(uploadpath + "/" + e.Result[i].Image, UriKind.Absolute));
                        Image image = new Image();
                        image.Source = img;
                        image.Cursor = Cursors.Hand;
                        image.Name = e.Result[i].ID.ToString();
                        image.MouseLeftButtonDown += new MouseButtonEventHandler(image_MouseLeftButtonDown);
                        sp.Children.Add(image);
                        if ((i + 1) % 4 == 0)
                        {
                            this.spList.Children.Add(sp);
                        }
                    }
                    if (e.Result.Count % 4 != 0)
                    {
                        this.spList.Children.Add(sp);
                    }
                    this.LayoutRoot.UpdateLayout();
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App app = (App)Application.Current;
            ProductDetail pd = new ProductDetail();
            pd.ID = Convert.ToInt32((sender as Image).Name);
            app.RedirectTo(pd);
        }
        #endregion

        #region 面包屑
        private void lnkSort_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            ProductList pl = new ProductList();
            pl.Sort = Convert.ToInt32((sender as HyperlinkButton).Tag);
            app.RedirectTo(pl);
        }

        private void InitTopNav(int id)
        {
            this.spTopNav.Children.Clear();
            ServiceClient clClass = CUtility.GetServiceClient();
            clClass.GetProductPathCompleted += new EventHandler<GetProductPathCompletedEventArgs>(clClass_GetProductPathCompleted);
            clClass.GetProductPathAsync(id);
        }

        void clClass_GetProductPathCompleted(object sender, GetProductPathCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (e.Result.Count > 0)
                {
                    for (int i = 1; i < e.Result.Count - 1; i++)
                    {
                        HyperlinkButton lnkbtnClass = new HyperlinkButton();
                        lnkbtnClass.Tag = e.Result[i][0];
                        lnkbtnClass.Content = e.Result[i][1];
                        lnkbtnClass.Style = Application.Current.Resources["styleNavLink"] as Style;
                        lnkbtnClass.Click += new RoutedEventHandler(lnkbtnClass_Click);
                        this.spTopNav.Children.Add(lnkbtnClass);

                        TextBlock tb = new TextBlock();
                        tb.Text = ">";
                        this.spTopNav.Children.Add(tb);
                    }
                    TextBlock tb1 = new TextBlock();
                    tb1.Text = e.Result[e.Result.Count - 1][1];
                    tb1.FontSize = 14;
                    this.spTopNav.Children.Add(tb1);
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void lnkbtnClass_Click(object sender, RoutedEventArgs e)
        {
            App ap = (App)Application.Current;
            ProductList pl = new ProductList();
            pl.Sort = this._sort;
            pl.ClassID = Convert.ToInt32((sender as HyperlinkButton).Tag);
            ap.RedirectTo(pl);
        }
        #endregion
        
        private void allProduct_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            HtmlWindow html = HtmlPage.Window;
            html.Navigate(new Uri("productlist.aspx", UriKind.Relative), "_blank");
        }

        private void lnkHome_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new Home());
        }

        private void lnkProduct_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new Product());
        }




    }
}
